from flask import Flask,request, jsonify
import pymysql
import pandas
from numpy import *
import uuid
import json
from flask_cors import *
import os
app = Flask(__name__)
CORS(app, supports_credentials=True)

@app.route('/look',methods=["POST"])
def returnresult():
    i=getinformation1()
    r=getinformation2()
    m=getinformation3()
    result_info={}
    page=json.loads(json.dumps(result_info))
    result1=[{'introduction':i,'raw':r,'method':m}]
    page['result1']=result1
    result=json.dumps(page, ensure_ascii=False)
    return result
def getinformation1():
    json_data=json.loads(request.get_data().decode('utf-8'))
    getnum=json_data.get('pagenum')
    mysql = pymysql.connect ("localhost", "root","gly123", "caipu")
    sql=('select introduction from page where user_name="%s"'%getnum)
    cursor=mysql.cursor(pymysql.cursors.DictCursor)
    cursor.execute(sql)
    password=cursor.fetchone()
    i=password.get('introduction')
    return i
def getinformation2():
    json_data=json.loads(request.get_data().decode('utf-8'))
    getnum=json_data.get('pagenum')
    mysql = pymysql.connect ("localhost", "root","gly123", "caipu")
    sql=('select raw from page where user_name="%s"'%getnum)
    cursor=mysql.cursor(pymysql.cursors.DictCursor)
    cursor.execute(sql)
    password=cursor.fetchone()
    r=password.get('raw')
    return r
def getinformation3():
    json_data=json.loads(request.get_data().decode('utf-8'))
    getnum=json_data.get('pagenum')
    mysql = pymysql.connect ("localhost", "root","gly123", "caipu")
    sql=('select method from page where user_name="%s"'%getnum)
    cursor=mysql.cursor(pymysql.cursors.DictCursor)
    cursor.execute(sql)
    password=cursor.fetchone()
    m=password.get('method')
    return m
@app.route('/login',methods=["POST"])
def getlogin():
    u=str(uuid.uuid1())
    json_data=json.loads(request.get_data().decode('utf-8'))
    getusername=json_data.get('email')
    getsecret=json_data.get('password')
    mysql = pymysql.connect ("localhost", "root","gly123", "caipu")
    sql=('select userpass from user where username="%s"'%getusername)
    cursor=mysql.cursor(pymysql.cursors.DictCursor)
    cursor.execute(sql)
    password=cursor.fetchone()
    a=password
    if a==getsecret:
        message='1'
    else:
        message='0'
    result_info={}
    login=json.loads(json.dumps(result_info))
    login1=[{'uid':u,'status':message}]
    login['login1']=login1
    result=json.dumps(login, ensure_ascii=False)
    return result
@app.route('/register',methods=["POST"])
def userregister():
    json_data=json.loads(request.get_data().decode('utf-8'))
    getusername=json_data.get('userlog')
    getpassword1=json_data.get('email')
    getpassword2=json_data.get('mima')
    mysql = pymysql.connect ("localhost", "root","gly123", "caipu")
    if getpassword1==getpassword2:
        sql1=('select count(*) from user')
        cursor=mysql.cursor(pymysql.cursors.DictCursor)
        cursor.execute(sql1)
        user_id=cursor.fetchone()
        userid=user_id.get('count(*)')+1
        sql2=('insert into user (usernum,username,userpass) values (%s,%s,%s)')
        cursor=mysql.cursor(pymysql.cursors.DictCursor)
        cursor.execute(sql2,[str(userid),str(getusername),str(getpassword1)])
        mysql.commit()
        message='well'
    else:
        message='wrong'
    result_info={}
    register=json.loads(json.dumps(result_info))
    register1=[{'register':message}]
    register['register1']=register1
    result=json.dumps(register, ensure_ascii=False)
    return result
@app.route('/exit',methods=["POST"])
def history():
    json_data=json.loads(request.get_data().decode('utf-8'))
    getusername=json_data.get('userlog')
    getpagenum=json_data.get('pagenum')
    mysql = pymysql.connect ("localhost", "root","gly123", "caipu")
    sql1=('create table %s(username char(10））'%getusername)
    cursor=mysql.cursor(pymysql.cursors.DictCursor)
    cursor.execute(sql1)
    sql2=('select count(1) from %s.coulmns'%getusername)
    cursor=mysql.cursor(pymysql.cursors.DictCursor)
    cursor.execute(sql2)
    num=cursor.fetchone()
    numr=num.get('count(1)')
    sql3=('alter table %s add coulmn history%s char(10)')
    cursor=mysql.cursor(pymysql.cursors.DictCursor)
    cursor.execute(sql3,[str(getusername),str(getpagenum)])
    sql4=('insert into %s (username,history%s) values (%s,%s)')
    cursor=mysql.cursor(pymysql.cursors.DictCursor)
    cursor.execute(sql4,[str(getusername),str(numr),str(getusername),str(getpagenum)])
    result='ok'
    return result
@app.route('/history',methods=["POST"])
def lookhistory():
    json_data=json.loads(request.get_data().decode('utf-8'))
    getusername=json_data.get('username')
    gethistorynum=json_data.get('historynum')
    mysql = pymysql.connect ("localhost", "root","gly123", "caipu")
    sql1=('select history%s from %s where history%s=%s')
    cursor=mysql.cursor(pymysql.cursors.DictCursor)
    cursor.execute(sql1,[str(gethistorynum),str(getusername),str(gethistorynum)])
    num=cursor.fetchone()
    return num
if __name__ == '__main__':
    app.run(host="192.168.31.24", debug=True, port=4200)